package com.blog.boot.webapp.controller.index;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.api.entity.ArticleEnity;
import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.api.system.service.ArticleService;
import com.blog.boot.api.system.service.UserService;
import com.blog.boot.webapp.controller.common.BaseController;
import com.blog.boot.webapp.utils.CommonUtils;
import com.blog.boot.webapp.utils.UserOperation;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @dsescription :首页
 * @author : cailong
 * @time : Created in 上午 11:00 2018/12/7 0007
 */
@Controller
public class IndexController extends BaseController {
    private static final String VIEW_PATH = "/index/views/";

    @Autowired
    GroupTemplate groupTemplate;
    @Autowired
    ArticleService articleService;
    @Autowired
    UserService userService;
    //首页
    @RequestMapping("/index.html")
    public RestMsg.RestModelAndView index(HttpServletRequest request) {
        Map<String,Object> shared = new HashMap<>();
        shared.put("title_suffix","修改的数据");
        request.getSession().setAttribute("currpage","index");
        groupTemplate.setSharedVars(shared);
        return restFactory.setModelAndView(VIEW_PATH+"index.html");
    }
    //文章详情
    @RequestMapping("/info/{module}/{id}.html")
    public RestMsg.RestModelAndView article(@PathVariable("module") String module ,@PathVariable("id") String id, HttpServletRequest request) {
        if(Objects.isNull(module) || Objects.isNull(id)){
            return restFactory.setModelAndView("forword:/error/404.html");
        }
        Long _id = null;
        try {
            _id = Long.valueOf(id);
        } catch (NumberFormatException e) {
            return restFactory.setModelAndView("forword:/error/404.html");
        }
        ArticleEnity articleByid = articleService.getArticleByid(_id);
        if( Objects.isNull(articleByid)){
            return restFactory.setModelAndView("forword:/error/404.html");
        }
        //如果是在审核状态 或者是删除状态 也将不可看
        if( articleByid.getStatus() == -1 || articleByid.getStatus() == -2){
            return restFactory.setModelAndView("forword:/error/404.html");
        }
        UserEntity user = userService.findUserByUserId(Long.valueOf(articleByid.getUid()));

        Template template = groupTemplate.getTemplate("/content.js");
        Object content = CommonUtils.execJavaScript(template.render(), "content", articleByid.getContent());
        request.setAttribute("html_content",content);
        request.setAttribute("article",articleByid);
        request.setAttribute("article_user",user);
        request.getSession().setAttribute(UserOperation.CURRPAGE,"jie");
        return restFactory.setModelAndView(VIEW_PATH+"info/article.html");
    }

    //文章详情
    @RequestMapping("/include/layout.html")
    public RestMsg.RestModelAndView layout() {
        return restFactory.setModelAndView("/include/layout.html");
    }

    //404
    @RequestMapping("/error/404.html")
    public RestMsg.RestModelAndView notFun() {
        return restFactory.setModelAndView(VIEW_PATH+"/error/404.html");
    }

    //500
    @RequestMapping("/error/500.html")
    public RestMsg.RestModelAndView error() {
        return restFactory.setModelAndView(VIEW_PATH+"/error/404.html");
    }


}
